package com.example.travel_front.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.travel_front.Model.Chat;
import com.example.travel_front.R;

import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {

    private final List<Chat> chatList;

    public interface OnChatClickListener {
        void onChatClick(Chat chat);
    }

    private final OnChatClickListener listener;

    public ChatAdapter(List<Chat> chatList,OnChatClickListener listener) {
        this.chatList = chatList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Chat chat = chatList.get(position);
        holder.titleText.setText(chat.getTitle());
        holder.subtitleText.setText(chat.getSubtitle());
        holder.timestamp.setText(chat.getTimestamp());
        Glide.with(holder.itemView.getContext()).load(chat.getImageUrl()).error(R.drawable.ic_traveler_avatar).into(holder.imageUrl);
        // 设置头像等其他视图属性
        // 绑定数据到视图
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onChatClick(chat);
            }
        });
    }

    @Override
    public int getItemCount() {
        return chatList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleText;
        TextView subtitleText;
        TextView timestamp;
        ImageView imageUrl;
        // 其他视图元素

        public ViewHolder(View itemView) {
            super(itemView);
            titleText = itemView.findViewById(R.id.title_text);
            subtitleText = itemView.findViewById(R.id.subtitle_text);
            timestamp = itemView.findViewById(R.id.timestamp);
            imageUrl = itemView.findViewById(R.id.avatar_image);
            // 初始化其他视图元素
        }
    }
}
